#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/log"
require "#{LKP_SRC}/lib/string_ext"

mtotal = nil
munit = nil
mused = []

$stdin.each_line do |line|
  line = line.resolve_invalid_bytes

  case line

  # /proc/meminfo is replaced as 'test_klp_atomic_replace: this has been live patched'
  # from time to time in kernel-selftests/livepatch testing. ignore them.
  when /^test_klp_atomic_replace:/
    next
  when /^time:/
    puts line
  else
    key, value, unit = line.split
    key = key.chomp(':')
    puts "#{key}: #{value}"
    value = value.to_i
    mtotal ||= value if key == 'MemTotal'
    if key == 'MemFree'
      mused << mtotal - value
      munit ||= unit
      puts "Memused: #{mused.last}"
      puts "max_used_#{munit}: #{mused.max}"
    end
  end
end
